package com.itheima.day01;

public class MyRunnable implements Runnable {
    private static int ticket=100;
    @Override
    public void run() {
       while (true){
           if ("窗口一".equals(Thread.currentThread().getName())){
               boolean result=synchronizedMethod();
               if (result){
                   break;
               }
           }
           if ("窗口二".equals(Thread.currentThread().getName())){
               synchronized (this){
                   if (ticket==0){
                       break;
                   }else{
                       try {
                           Thread.sleep(100);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       ticket--;
                       System.out.println(Thread.currentThread().getName()+"在买票，还剩"+ticket+"张票");
                   }
               }
           }
       }
    }

    private synchronized boolean synchronizedMethod() {
        if (ticket==0){
            return true;
        }else{
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket--;
            System.out.println(Thread.currentThread().getName()+"在买票，还剩"+ticket+"张票");
            return false;
        }
    }
}
